<?php

require_once './library/Zend/Db/Table/Abstract.php';

class TableComposant extends Zend_Db_Table
{

    protected $_name;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;

    protected function _setup()
    {
        $this->_name = 'composants';
        $this->_primary = 'IdComposant';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    public function getLstComposants()
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result))
            return $result;
    }

    public function getComposant($pIpComposant)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('Ip = ?', $pIpComposant);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function getComposantName($pNameComposant)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('HostName = ?', $pNameComposant);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        return $result;
    }

    public function getComposantId($pIdComposant)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('IdComposant = ?', $pIdComposant);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        return $result;
    }

    public function addComposant($pNomComposant, $pIpComposant, $pNbInterface, $pTimeConnect, $pModel)
    {
        $val = array('HostName' => $pNomComposant,
            'NbInterface' => $pNbInterface,
            'Ip' => $pIpComposant,
            'TimeConnect' => $pTimeConnect,
            'Model' => $pModel);
        $this->_db->insert($this->_name, $val);
        return $this->_db->lastInsertId($this->_name);
    }

    public function deleteComposant($pIdComposant)
    {
        $this->delete($this->_db->quoteInto('IdComposant = ?', $pIdComposant));
    }

    public function updateComposant($pIdComposant, $pNomComposant, $pIpComposant, $pNbInterface, $pTimeConnect, $pModel)
    {
        $val = array('HostName' => $pNomComposant,
            'NbInterface' => $pNbInterface,
            'Ip' => $pIpComposant,
            'TimeConnect' => $pTimeConnect,
            'Model' => $pModel);

        $this->update($val, $this->_db->quoteInto('IdComposant = ?', $pIdComposant));
    }

}
?>